package com.hongzhimei.yukicomic.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;



import java.util.ArrayList;
import java.util.List;

/**
 * Created by caoyang on 2016/12/20.
 */

public class BaseActivity extends AppCompatActivity {

    // 1. 共享资源
    private static List<BaseActivity> activities = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        synchronized (activities) {
            activities.add(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        synchronized (activities) {
            activities.remove(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    /**
     * 自杀
     */
    public void killAll() {
        //2.  1.CopyOnWriteArrayList
        List<BaseActivity> copy;
        synchronized (activities) {
            copy = new ArrayList<>(activities);
        }
        for (BaseActivity activity : copy) {
            activity.finish();
        }
        //退出当前进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}